package com.example.tellwin.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.apkfuns.logutils.LogUtils;
import com.example.tellwin.R;
import com.example.tellwin.question.act.WriteBroadActivity;

import org.w3c.dom.Text;

/**
 * @author : LinZePeng
 * @date : 2020/12/15
 * @Describe ：
 **/
public class ListPopuwindow extends PopupWindow {

    WriteBroadActivity.ToolType[] toolTypes;
    OnItemClickListPopListener onItemClickListPopListener;
    Context context;

    public ListPopuwindow(Context context, WriteBroadActivity.ToolType[] toolTypes, OnItemClickListPopListener onItemClickListPopListener) {
        super(context);
        this.onItemClickListPopListener = onItemClickListPopListener;
        this.context = context;
        this.toolTypes = toolTypes;
        init();
    }

    private void init() {
        View view = LayoutInflater.from(context).inflate(R.layout.list_pop, null);
        RecyclerView recyclerView = view.findViewById(R.id.list_rv);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        ListAdpater listAdpater = new ListAdpater();
        recyclerView.setAdapter(listAdpater);
        setContentView(view);
        setBackgroundDrawable(context.getResources().getDrawable(R.color.transparent));
        setWidth(context.getResources().getDimensionPixelOffset(R.dimen.dp_100));
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setOutsideTouchable(true);
    }

    public void show(View view) {
        showAsDropDown(view);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        LogUtils.e("dismiss");
    }

    class ListAdpater extends RecyclerView.Adapter<ListAdpater.MyViewHolder> {

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_list_text, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            WriteBroadActivity.ToolType toolType = toolTypes[position];
            holder.itemTv.setText(toolType.name);
            holder.itemTv.setOnClickListener(v -> {
                onItemClickListPopListener.onItemClick(position, toolType);
                dismiss();
            });
        }

        @Override
        public int getItemCount() {
            return toolTypes.length;
        }

        private class MyViewHolder extends RecyclerView.ViewHolder {

            TextView itemTv;

            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                itemTv = itemView.findViewById(R.id.item_tv);
            }
        }
    }

    public interface OnItemClickListPopListener {
        void onItemClick(int position, WriteBroadActivity.ToolType toolType);
    }
}
